Skip to content

feat: Cursor preCompact becomes a rius.compaction event (RIUS-1548) - #69

Merged
sudopower merged 1 commit into
mainfrom
factory/RIUS-1548
Oct 22, 2026
Merged

sudopower merged 1 commit into
mainfrom
factory/RIUS-1548

Conversation

@sudopower

Copy link
Copy Markdown
Member

Ticket: RIUS-1548 (M3). Follows rius-coding-agents #67 (Claude Code) and #68 (Codex).

What changed

  • Cursor's preCompact hook becomes a rius.compaction span event on the LLM span of the generation it interrupted: kind summarize, executor client, trigger (only auto or manual), tokens.before = context_tokens, context_window = context_window_size, messages.compacted = messages_to_compact, messages.kept = message_count - messages_to_compact when both are known and the result is not negative. Anything absent, non-numeric, bool or negative is left out.
  • The next LLM span carries gen_ai.conversation.compacted = true, and no other (as in A37). Cursor never reports a size after a compaction, so this flag is what tells a reader a drop in context is a compaction. A turn with no LLM span yet passes the flag on; a hook that names no turn still flags the next call; a subagent's compaction is not ours.
  • The existing cursor.context.* keys are unchanged (argus-core reads them as a marker).
  • Spans are rebuilt from the whole spool on every export, so the event and the flag do not depend on how hooks are batched; _digest already covers span.events, so a span is resent when only its event changes (test added).
  • Docs: docs/cursor.md (Compactions section), pointer in docs/how-it-works.md.

Real compaction: NOT checked

No real captured preCompact payload exists in the repo (the tests reuse the shape of the docs-derived docs_session.json), and a real Cursor run needs a login, so none was made. Per the ticket the releaser keeps this change out of the plugin release PR until a real check is recorded. The protobuf decode e2e test is skipped here (opentelemetry-proto is not installed); the event was checked in the OTLP JSON body.

Checks (head 918231c)

  • /usr/bin/python3 -m pytest -q tests/test_cursor_e2e.py tests/test_cursor_export.py tests/test_cursor_compaction.py: 74 passed, 1 skipped
  • factory-heavy 900 /usr/bin/python3 -m pytest -q: 1864 passed, 9 skipped
  • Broke and restored: the flag attribute (12 fail), clearing the owed flag (7 fail), total >= compacted (1 fails), events= on the LLM span (38 fail), the owed re-arm (the late-compaction test fails).

Challenger

Round 1: one P2 (a late compaction could leave the next call unflagged). It was not a bug: the flag is cleared and then re-armed by the same turn's compaction. I pinned that case with test_a_late_compaction_does_not_use_up_the_flag_it_owes_the_next_call and the second review passed. No waivers.

Assumptions

None new.

… LLM span is flagged (RIUS-1548)

The hook's trigger, context size, window and message counts become a rius.compaction event on the LLM span it interrupted; the cursor.context.* keys are unchanged. The next LLM span carries gen_ai.conversation.compacted, since Cursor never reports a size after. No real Cursor compaction was captured.
@sudopower

Copy link
Copy Markdown
Member Author

factory/review: PASS (round 1) @ 918231c

Putting the compaction on the interrupted LLM span as an event, and building it from the whole spool, is the right shape for Cursor. Batching then can't move the event or the flag, and _digest already covers span.events. Only enums and counts reach the event, and trigger is limited to auto/manual, so no content gets in. The "Real compaction: NOT checked" section is accurate: this machine has no Cursor spool data with a preCompact in it.

Verified

  • Head pinned with gh pr view = 918231c, 0 behind main.
  • /usr/bin/python3 -m pytest -q -p no:cacheprovider tests/test_cursor_e2e.py tests/test_cursor_export.py tests/test_cursor_compaction.py tests/test_cursor_spans.py: 103 passed, 2 skipped.
  • factory-heavy 900 /usr/bin/python3 -m pytest -q -p no:cacheprovider: 1864 passed, 9 skipped (Python 3.9.6, the floor). CI: 21 checks pass.
  • 18 mutants on cursor_spans.py: 15 killed, 3 survive (F1). Killed mutants include the subagent scope check, the orphan append, manual dropped, >= to >, each _count removed, the kind_oi == "LLM" reset, owed or, events=, the flag line, the executor and kept.
  • Probes, run in a scratch test and then removed:
    • Two orphan compactions before one turn flag only that turn ([True, None]), so while is correct.
    • An unanswered turn between two others: see F2.
  • Premise: the payload shape (generation_id on preCompact, the seven fields) comes from docs_session.json, which follows Cursor's hooks docs. Echo dedupe (ECHO_WINDOW_NS, 2 s, whole body) can't merge two real compactions, because is_first_compaction or the sizes differ. otlp.py already encodes span.events, since Codex spans use it.
  • Ticket: cursor.context.* keys are unchanged, messages.kept is sent only when both counts are known and it is not negative, and the flag goes on the next LLM span only.

Findings (none blocking)

F1 (P3): three surviving mutants in cursor_spans.py.

  • if value is not None: → if value: passes. A zero count, such as messages_to_compact: 0, would be dropped, and no test sends 0.
  • fold.orphan_compactions[0] <= turn["start_ns"] → < passes. No test has an orphan at the same ns as the next turn's start.
  • while (fold.orphan_compactions ... → if passes. Two orphans before one turn would flag the next two calls. My probe shows the code is right; please add a test that pins it.

F2 (P3, note): the flag moves to an unanswered turn at session end. In test_a_turn_without_an_llm_span_passes_the_flag_on, gen-0001 compacts, gen-0002 is prompted and never answered, then gen-0003 and gen-0004 run. While the session is open, gen-0003 is flagged ([None, True, None]). Once sessionEnd arrives, or final_ns is given, gen-0002 gets an LLM span and takes the flag ([None, True, None, None]), and gen-0003 is re-sent without it. The final state is defensible, because gen-0002 was the first call after the compaction. But the test only checks the mid-session state, and the docstring ("has no LLM span to carry the flag") holds only until the session ends. Please pin the closed-session result too, so the move is deliberate. One more case: a closing export with capture off (_unsent(closing_only=True), cursor_export.py:359) does not re-send spans that already went out finished. gen-0003 then keeps compacted=true in the backend while gen-0002 also gets it, so two calls are flagged. This needs a turn that is prompted and never gets stop, so it is rare.

F3 (P3, note): a preCompact after its generation's stop puts the event's time after the LLM span's end (test_a_compaction_after_the_turn_stopped_still_lands_on_its_span). OTLP allows this. If the console draws events on the span's timeline, it will show the event outside the bar. The same happens mid-turn: _llm_span ends at the latest response_ns, and exports run every EXPORT_EVERY_N_TOOLS, so a compaction after a response but before the next one is outside the span until a later export. The docs could also say that the event is not visible until the generation answers or stops.

F4 (P3): the no-content test for preCompact runs only with capture off. test_precompact_carries_no_content_into_the_spool_or_the_event uses capture=False. With capture on, to_record keeps CONTENT_FIELDS by field name, so a prompt or text field on a preCompact would be spooled. The event itself is safe, because _compaction_event reads only named counts and an enum. Cursor's docs list no such field, so this is a test gap, not a leak.

Not verified

  • A real Cursor compaction. There is no Cursor spool data with preCompact on this machine, and a real run needs a Cursor login. The payload is the docs shape only, so a different trigger value or a missing generation_id in practice is untested. Per the ticket, the releaser keeps this out of the plugin release until a real check is recorded.
  • The protobuf OTLP path: test_cursor_e2e skips it here (opentelemetry-proto not installed). I relied on the JSON body test and on otlp.py already encoding Codex events.
  • Sonnet challenger: it confirmed F1-F3, extended F2 (the closing-only case) and F3 (mid-turn), raised F4, and found no bug in the owed-flag loop, the subagent scoping or the OTLP event encoding. Its notes on unsanitized cursor.context.* are left out: they predate this PR, and the ticket keeps those keys unchanged.
  • How argus-core reads rius.compaction events on LLM spans (rule C). That is backend work, outside this PR.

@sudopower
sudopower merged commit 35d1b5e into main Oct 22, 2026
22 checks passed
@sudopower
sudopower deleted the factory/RIUS-1548 branch October 22, 2026 07:25
@sudopower sudopower mentioned this pull request Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant